<?php

/**
 * myCredentials form.
 *
 * @package    docplus.co.za
 * @subpackage form
 * @author     Fox Matuku
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class myCredentialsForm extends sfForm {
	private $requiredFields = array('email');

	public function configure() {

		$this->widgetSchema['user_id'] = new sfWidgetFormInputHidden();
		$this->widgetSchema['email'] = $this->getOption("is_email_hidden", false) ? new sfWidgetFormInputHidden() : new sfWidgetFormInputText();
		$this->widgetSchema['password'] = new sfWidgetFormInputPassword(array(), array("autocomplete" => "off"));
		$this->widgetSchema['password_confirm'] = clone $this->widgetSchema['password'];

		$this->validatorSchema['user_id'] = new sfValidatorString(array('required' => false));
		$this->validatorSchema['email'] = new sfValidatorEmail(array("required" => true), array("invalid" => "Your *email* is invalid.", "required" => "Your *email* is required email."));
		$this->validatorSchema['password'] = new sfValidatorString(array('max_length' => 128, 'required' => $this->getOption("is_password_required", false)), array('required' => 'Your *password* is required.'));
		$this->validatorSchema['password_confirm'] = clone $this->validatorSchema['password'];
		$this->validatorSchema['password_confirm']->setMessage('required', 'Your *confirm password* is required.');

		$this->validatorSchema->setPostValidator(new sfValidatorCallback(array('callback' => array($this, '_emailAlreadyExistsValidator'))));

		$this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_confirm', array(), array('invalid' => 'Your *two passwords* must be the same.')));

		$this->widgetSchema->setNameFormat('gf_user[%s]');
	}

	public function _emailAlreadyExistsValidator($validator, $values, $arguments) {
		foreach ($this->requiredFields as $field) {
			try {
				$this->validatorSchema[$field]->clean($values[$field]);
			}
			catch (sfValidatorErrorSchema $e) {
				return $values;
			}
			catch (sfValidatorError $e) {
				return $values;
			}
		}

		// try to fetch the registered user
		$gfUser = GfUserTable::getInstance()->findOneByEmail($values['email']);

		// check for the existence of this email
		if (is_object($gfUser) && $gfUser->getId() != $values["user_id"]) {
			$this->validatorSchema['email']->setMessage('invalid', 'This email already exists.');
		}
		else {
			return $values;
		}

		// since we've reached here seems the supplied email exists already
		throw new sfValidatorErrorSchema($this->validatorSchema['email'], array('email' => new sfValidatorError($this->validatorSchema['email'], 'invalid')));
		return $values;
	}

	/**
	 * Override the save method to save the user.
	 */
	public function save($con = null) {

		// fetch the passed on user
		$gfUser = $this->getOption("user");

		// check for both the existence of a user with this id
		if (is_object($gfUser)) {
			$gfUser->setAlgorithm('md5');
			$gfUser->setEmail($this->getValue("email"));

			$password = $this->getValue("password");

			// make sure we don't save empty passwords
			if (!empty($password)) {
				$gfUser->setPassword($password);
			}
			$gfUser->save();
		}
	}

}
